<template>
  <div class="bg-white rounded-lg p-5 h-full">
    <div class="font-bold text-lg mb-4">医疗卫生基本信息</div>

    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入医疗机构名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item prop="town">
        <el-select
          v-model="queryParams.town"
          filterable
          remote
          clearable
          reserve-keyword
          placeholder="输入街道名称搜索"
          :remote-method="remoteMethod"
        >
          <el-option
            v-for="item in streetOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item prop="medicalType">
        <el-select
          v-model="queryParams.medicalType"
          placeholder="请选择机构类别"
          style="width: 100%"
        >
          <el-option
            v-for="dict in dict.type.enterprise_info_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item prop="medicalLevel">
        <el-select
          v-model="queryParams.medicalLevel"
          placeholder="医疗机构等级"
        >
          <el-option
            v-for="item in dict.type.medical_level"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="recordStatus">
        <el-select v-model="queryParams.recordStatus" placeholder="备案状态">
          <el-option
            v-for="item in dict.type.medical_record_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="registerStatus">
        <el-select
          v-model="queryParams.registerStatus"
          placeholder="是否注册平台"
        >
          <el-option
            v-for="item in dict.type.yes_or_no"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb-4">
      <el-button type="primary" @click="search">搜索</el-button>
      <el-button @click="reset">重置</el-button>

      <el-button type="primary" @click="toEditPage">添加</el-button>

      <el-button type="primary" @click="importBtn">导入</el-button>

      <el-button type="primary" @click="downloadTemplateBtn"
        >下载导入模板</el-button
      >

      <el-button type="primary" @click="syncBtn">同步</el-button>
      <!-- <el-button type="primary" @click="toDoctorPage">医师</el-button> -->
      <!-- <el-button type="primary" @click="toNursePage">护士</el-button> -->
    </el-row>

    <el-table border v-loading="loading" :data="dataList">
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column
        label="医疗机构名称"
        align="center"
        prop="enterpriseName"
      >
        <template #default="scope">
          <el-button type="text" @click="toDetailPage(scope.row)">{{
            scope.row.enterpriseName
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="累计记分" align="center" prop="scoring">
        <template #default="scope">
          <div>{{ scope.row.scoring }} 分</div>
        </template>
      </el-table-column>
      <el-table-column label="机构类别" align="center" prop="medicalOrgType">
        <template #default="scope">
          <dict-tag
            :options="dict.type.medical_org_type"
            :value="scope.row.medicalOrgType"
          />
        </template>
      </el-table-column>
      <el-table-column label="所在区/县" align="center" prop="county" />
      <el-table-column label="镇/街道" align="center" prop="town" />
      <el-table-column
        label="医疗等级"
        width="80"
        align="center"
        prop="medicalLevel"
      >
        <template #default="scope">
          <dict-tag
            :options="dict.type.medical_level"
            :value="scope.row.medicalLevel"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="备案状态"
        width="80"
        align="center"
        prop="recordStatus"
      >
        <template #default="scope">
          <dict-tag
            :options="dict.type.medical_record_status"
            :value="scope.row.recordStatus"
          />
        </template>
      </el-table-column>
<!--      <el-table-column
        label="是否注册平台"
        align="center"
        width="120"
        prop="registerStatus"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yes_or_no"
            :value="scope.row.registerStatus"
          />
        </template>
      </el-table-column>-->
      <el-table-column
        label="操作"
        align="center"
        class-name="fixed-width"
        width="350"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="toSelfInspectPage(scope.row)"
            >机构自查自纠</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="toPrescriptionPage(scope.row)"
            >处方权限授权</el-button
          >
          <el-button size="mini" type="text" @click="toDetailPage(scope.row)"
            >查看详情</el-button
          >
          <el-button size="mini" type="text" @click="lookSortBtn(scope.row)"
            >查看记分情况</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getDataList"
    />
  </div>
</template>

<script>
import {
  listOrg,
  getOrg,
  delOrg,
  addOrg,
  updateOrg,
} from "@/api/zhangping/zpMedicalOrg";
import { getHospitalListAPI } from "@/api/zhangping/health-care/base-info";
import { getZpStreetListApi } from "@/api/common";
export default {
  dicts: [
    "yes_or_no",
    "medical_record_status",
    "medical_level",
    "enterprise_info_type",
    "medical_org_type",
  ],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医疗机构信息表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
        creditCode: null,
        scoring: null,
        medicalLevel: null,
        recordStatus: null,
        recordStart: null,
        recordEnd: null,
        recordCertificates: null,
        physicianNum: null,
        nurseNum: null,
        createId: null,
        updateId: null,
      },
      streetOptions: [],
      options: [],
      value: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      detailForm: {},
      isEdit: false,
      editVisible: false,
    };
  },
  created() {
    this.getDataList();
    this.remoteMethod();
  },
  methods: {
    remoteMethod(query) {
      // const town = this.queryParams.town;
      // console.log(town);

      const params = {
        mname: query,
      };
      console.log(params);

      getZpStreetListApi(params).then((response) => {
        if (response.code === 200) {
          this.streetOptions = response.data.map((item) => {
            return {
              value: item.mname,
              label: item.mname,
            };
          });
        }
      });
    },
    toDoctorPage() {
      this.$router.push("/health-care/doctor-detail");
    },
    toNursePage() {
      this.$router.push("/health-care/nurse-detail");
    },
    toSelfInspectPage(row) {
      const obj = {
        path: "/health-care/self-inspect",
        query: {
          creditCode: row.creditCode,
        },
      };
      this.$router.push(obj);
    },
    toPrescriptionPage(row) {
      const obj = {
        path: "/health-care/prescription",
        query: {
          n: row.narcoticStatus,
          creditCode: row.creditCode,
        },
      };
      this.$router.push(obj);
    },
    toDetailPage(row) {
      const { id } = row;
      console.log(id);

      const obj = {
        path: "/health-care/base-info-detail",
        query: {
          creditCode: row.creditCode,
        },
      };
      this.$router.push(obj);
    },
    lookDetailBtn() {
      const obj = {
        path: "/healthCare/zpMedicalOrg/detail",
        query: {
          id: "111",
        },
      };
      this.$router.push(obj);
    },
    lookSortBtn() {},
    importBtn() {},
    downloadTemplateBtn() {},
    syncBtn() {},
    toEditPage() {
      this.$router.push("/health-care/base-info-edit");
    },
    addBtn() {
      const path = "/healthCare/zpMedicalOrg/detail";
      this.$router.push(path);
    },
    /** 查询医疗机构信息列表 */
    getDataList() {
      this.loading = true;
      getHospitalListAPI(this.queryParams)
        .then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.$refs.queryForm.resetFields();
      // this.resetForm("form");
      this.search();
    },
    /** 搜索按钮操作 */
    search() {
      this.queryParams.pageNum = 1;
      this.getDataList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.detailForm = {};
      this.isEdit = false;
      this.editVisible = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      // this.reset();
      const id = row.id || this.ids;
      getOrg(id).then((response) => {
        this.detailForm = response.data;
        if (type == 1) {
          this.isEdit = true;
        } else {
          // 查看
          this.isEdit = false;
        }
        this.editVisible = true;
      });
    },
    /** 提交按钮 */
    submitForm(form, id) {
      if (id != null) {
        updateOrg(form).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.editVisible = false;
          this.getDataList();
        });
      } else {
        addOrg(form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.editVisible = false;
          this.getDataList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除医疗机构信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delOrg(ids);
        })
        .then(() => {
          this.getDataList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "zpMedical/org/export",
        {
          ...this.queryParams,
        },
        `org_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  .tableBtnListCss {
    margin-top: 10px;
    border-top: 1px solid #e0e0e0;
    padding-top: 10px;
  }
}
</style>
